home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Behaviors / Actions / Set Nav Bar Image.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  15.8 KB  |  515 lines

  1. //*************** GLOBALS VARS *****************
  2.  
  3. var helpDoc = MM.HELP_behSetNavBarImage;
  4.  
  5. var NAVBAR_ELEM_DATA = '';
  6. var NAVBAR_ELEM_VIEW = '';
  7.  
  8. var T = ''; // TabControl object
  9.  
  10.  
  11. //******************* BEHAVIOR FUNCTIONS **********************
  12.  
  13. // MM_nbGroup('init', grpName, imgName, upSrc, [imgName, downSrc]+, preload)
  14. // MM_nbGroup('over',[imgName, overSrc, overDownSrc]+, preload)
  15. // MM_nbGroup('out')
  16. // MM_nbGroup('down', grpName, [imgName, downSrc]+, preload)
  17. //
  18. function MM_nbGroup(event, grpName) { //v3.0
  19.   var i,img,nbArr,args=MM_nbGroup.arguments;
  20.   if (event == "init" && args.length > 2) {
  21.     if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
  22.       img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
  23.       if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
  24.       nbArr[nbArr.length] = img;
  25.       for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
  26.         if (!img.MM_up) img.MM_up = img.src;
  27.         img.src = img.MM_dn = args[i+1];
  28.         nbArr[nbArr.length] = img;
  29.     } }
  30.   } else if (event == "over") {
  31.     document.MM_nbOver = nbArr = new Array();
  32.     for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
  33.       if (!img.MM_up) img.MM_up = img.src;
  34.       img.src = (img.MM_dn && args[i+2]) ? args[i+2] : args[i+1];
  35.       nbArr[nbArr.length] = img;
  36.     }
  37.   } else if (event == "out" ) {
  38.     for (i=0; i < document.MM_nbOver.length; i++) {
  39.       img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  40.   } else if (event == "down") {
  41.     if ((nbArr = document[grpName]) != null)
  42.       for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
  43.     document[grpName] = nbArr = new Array();
  44.     for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
  45.       if (!img.MM_up) img.MM_up = img.src;
  46.       img.src = img.MM_dn = args[i+1];
  47.       nbArr[nbArr.length] = img;
  48.   } }
  49. }
  50.  
  51. document.VERSION_MM_nbGroup = 3.0; //define latest version number for behavior inspector
  52.  
  53. //******************* API **********************
  54.  
  55.  
  56. //Checks for the existence of images.
  57. //If none exist, returns false so this Action is grayed out.
  58.  
  59. function canAcceptBehavior(behTag){
  60.   var retVal = false;
  61.   var selObj = dw.getBehaviorElement();
  62.   if (!selObj && behTag)
  63.     selObj = dw.getDocumentDOM().getSelectedNode();
  64.   if (selObj && selObj.tagName && selObj.tagName == "IMG")
  65.     retVal = true;
  66.   else if (selObj && selObj.tagName && selObj.tagName == "A" &&
  67.            selObj.getElementsByTagName("IMG").length > 0)
  68.     retVal = true;
  69.   else if (selObj && selObj.tagName && selObj.tagName == "AREA")
  70.     retVal = true;
  71.   if (retVal) retVal = "(onClick),(onMouseOver),(onMouseOut),onLoad";
  72.   return retVal;
  73. }
  74.  
  75.  
  76. //Returns Javascript functions to be inserted in HTML head with script tags.
  77.  
  78. function behaviorFunction(){
  79.   return "MM_preloadImages,MM_findObj,MM_nbGroup"
  80. }
  81.  
  82.  
  83.  
  84. //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
  85.  
  86. function applyBehavior() {
  87.   var behEvent, behObj, retVal = '';
  88.  
  89.   if (!NAVBAR_ELEM_DATA.isComplete()) {
  90.  
  91.     retVal = MSG_MissingData;
  92.  
  93.   } else {
  94.  
  95.     if (!dw.isReapplyingBehaviors()) {
  96.       T.finish();
  97.     }
  98.       
  99.     behEvent = dw.getBehaviorEvent();
  100.     behObj = dw.getBehaviorElement();
  101.     
  102.     if (behObj) NAVBAR_ELEM_DATA.tagObj = behObj;
  103.       
  104.     NAVBAR_ELEM_DATA.updateObjectTag(behEvent);
  105.  
  106.     switch ( behEvent ){
  107.     case "onLoad":
  108.       retVal = NAVBAR_ELEM_DATA.getOnLoadCall();
  109.       break;
  110.     case "onMouseOver":
  111.       retVal = NAVBAR_ELEM_DATA.getOnMouseOverCall();
  112.       break;
  113.     case "onMouseOut":
  114.       retVal = NAVBAR_ELEM_DATA.getOnMouseOutCall();
  115.       break;
  116.     case "onClick":
  117.       retVal = NAVBAR_ELEM_DATA.getOnClickCall();
  118.       break;
  119.     default:
  120.     }
  121.     updateBehaviorFns("MM_preloadImages,MM_findObj,MM_nbGroup");
  122.     
  123.   }
  124.   return retVal;
  125. }
  126.  
  127.  
  128. //Returns a dummy function call to inform Dreamweaver the type of certain behavior
  129. //call arguments. This information is used by DW to fixup behavior args when the
  130. //document is moved or changed.
  131. //
  132. //It is passed an actual function call string generated by applyBehavior(), which
  133. //may have a variable list of arguments, and this should return a matching mask.
  134. //
  135. //The return values are:
  136. //  URL     : argument could be a file path, which DW will update during Save As...
  137. //  NS4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  138. //  IE4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  139. //  other...: argument is ignored
  140.  
  141. function identifyBehaviorArguments(fnCallStr) {
  142.   var argList = '';
  143.   var args = extractArgs(fnCallStr);
  144.   if (args[1] == 'init' || args[1] == 'down') {
  145.     argList = "other,other";
  146.     for (i=3; i+1 < args.length; i+=2)
  147.       argList += ",objName,DEP";
  148.   } else if (args[1] == 'over') {
  149.     argList = "other";
  150.     for (i=2; i+2 < args.length; i+=3)
  151.       argList += ",objName,DEP,DEP";
  152.   } else if (args[1] == 'out') {
  153.     argList = "other";
  154.   }
  155.   return argList;
  156. }
  157.  
  158.  
  159. //Given the original function call, this parses out the args and updates
  160. //the code. If there's a preload id at the end of the arglist, deletes
  161. //the preload handler.
  162.  
  163. function deleteBehavior(behFnCallStr) {
  164.   var args,imgList = new Array();
  165.   //Maybe remove preload handler
  166.   args = extractArgs(behFnCallStr);//get new list of imgObj,imgSrc pairs
  167.   if (args[1] == 'init' && args[args.length-1] != '0') {
  168.     for (var i=2; i+1 < args.length-1; i+=2)
  169.       imgList.push(args[i+1]);
  170.   } else if (args[1] == 'down' && args[args.length-1] != '0') {
  171.     for (var i=3; i+1 < args.length-1; i+=2)
  172.       imgList.push(args[i+1]);
  173.   } else if (args[1] == 'over' && args[args.length-1] != '0') {
  174.     for (var i=2; i+2 < args.length-1; i+=3) {
  175.       imgList.push(args[i+1]);
  176.       imgList.push(args[i+2]);
  177.     }
  178.   }
  179.   if (imgList.length > 0)
  180.     preloadUpdate("", imgList, 0);
  181. }
  182.  
  183.  
  184.  
  185. //***************** LOCAL FUNCTIONS  ******************
  186.  
  187. //Load the UI with image names.
  188.  
  189. function initializeUI() {
  190.   var behObj;
  191.  
  192.   NAVBAR_ELEM_DATA = new NavBarElem('document','group1');
  193.   
  194.   behObj = dw.getBehaviorElement();
  195.   if (!behObj && dw.getBehaviorTag())
  196.     behObj = dw.getDocumentDOM().getSelectedNode();
  197.   if (behObj && (behObj.tagName == "IMG" || 
  198.                  behObj.tagName == "A" || 
  199.                  behObj.tagName == "AREA")) {
  200.     if (behObj.tagName == "IMG" && behObj.parentNode.tagName == "A")
  201.       behObj = behObj.parentNode;
  202.       
  203.     NAVBAR_ELEM_DATA.parseObjectTag(behObj);
  204.  
  205.     if (!dw.isReapplyingBehaviors()) {  // only setup UI if not re-applying
  206.  
  207.       NAVBAR_ELEM_VIEW = new NavBarElemView(document, NAVBAR_ELEM_DATA);
  208.  
  209.  
  210.       // Initialize the TabControl.  (Pass in the prefix used for the tab layers)
  211.       T = new TabControl('Tab');
  212.  
  213.       // Add tab pages.
  214.       T.addPage('page1', new ElemControl(LABEL_Basic));
  215.       T.addPage('page2', new AdvControl(LABEL_Advanced));
  216.  
  217.       // Initialize and display the tabs.
  218.       T.start();
  219.   } }
  220. }
  221.  
  222.  
  223. function browseFile(itemName, isImage) {
  224.   var item = findObject(itemName);
  225.   if (item != null) {
  226.     var result = dw.browseForFileURL('select', (isImage) ? LABEL_getImage : LABEL_getURL, isImage);
  227.     if (result) {
  228.       item.value = result;
  229.       T.update(itemName);
  230.   } }
  231. }
  232.  
  233.  
  234. /* ------------------------------------------------------------------*/
  235.  
  236. function ElemControl(theTabLabel) {
  237.   this.tabLabel = theTabLabel;
  238. }
  239.  
  240. ElemControl.prototype.getTabLabel = ElemControl_getTabLabel;
  241. ElemControl.prototype.load = ElemControl_load;
  242. ElemControl.prototype.update = ElemControl_update;
  243.  
  244. function ElemControl_getTabLabel() {
  245.   return this.tabLabel;
  246. }
  247.  
  248. function ElemControl_load() {
  249.   NAVBAR_ELEM_VIEW.display();
  250. }
  251.  
  252. function ElemControl_update(itemName) {
  253.   NAVBAR_ELEM_VIEW.update(itemName);
  254. }
  255.  
  256.  
  257. /* ------------------------------------------------------------------*/
  258.  
  259. function AdvControl(theTabLabel) {
  260.   this.tabLabel = theTabLabel;
  261.   this.imgEvent = new ListControl('imgEvent');
  262.   this.imgEvent.setAll(MENULIST_imageEvents);
  263.   this.downImgLabelObj = findObject('downImgLabel');
  264.   this.downImgLabel = this.downImgLabelObj.innerHTML;
  265.   this.downImgInputObj = findObject('downImgInput');
  266.   this.downImgInput = this.downImgInputObj.innerHTML;
  267.   this.downList = new ElemListView('elemList', NAVBAR_ELEM_DATA.downImages, 1);
  268.   this.overList = new ElemListView('elemList', NAVBAR_ELEM_DATA.overImages, 2);
  269. }
  270.  
  271. AdvControl.prototype.getTabLabel = AdvControl_getTabLabel;
  272. AdvControl.prototype.load        = AdvControl_load;
  273. AdvControl.prototype.update      = AdvControl_update;
  274. AdvControl.prototype.display     = AdvControl_display;
  275. AdvControl.prototype.lastUnload  = AdvControl_lastUnload;
  276.  
  277. function AdvControl_getTabLabel() {
  278.   return this.tabLabel;
  279. }
  280.  
  281. function AdvControl_load() {
  282.   var nameSpan = findObject('nameSpan');
  283.   if (nameSpan) {
  284.     T.obj.visibility = 'hidden';  // work around DW3 bug.
  285.     nameSpan.innerHTML = NAVBAR_ELEM_DATA.imgName;
  286.     T.obj.visibility = 'visible';
  287.   }
  288.   this.display();
  289. }
  290.  
  291. function AdvControl_display() {
  292.   if (this.imgEvent.getIndex() == 0) {
  293.     // show extra control
  294.     T.obj.visibility = 'hidden';  // work around DW3 bug.
  295.     this.downImgLabelObj.innerHTML = this.downImgLabel;
  296.     this.downImgInputObj.innerHTML = this.downImgInput;
  297.     T.obj.visibility = 'visible';
  298.     this.overList.load();
  299.     this.overList.display();
  300.   } else {
  301.     // hide extra control
  302.     T.obj.visibility = 'hidden';  // work around DW3 bug.
  303.     this.downImgLabelObj.innerHTML = '';
  304.     this.downImgInputObj.innerHTML = '';
  305.     T.obj.visibility = 'visible';
  306.     this.downList.load();
  307.     this.downList.display();
  308.   }
  309. }
  310.  
  311. function AdvControl_update(itemName) {
  312.  
  313.   if (itemName == 'imgEvent') {
  314.  
  315.     this.display();
  316.  
  317.   } else if (itemName == 'elemList' ||
  318.              itemName == 'file0' ||
  319.              itemName == 'file1') {
  320.     if (this.imgEvent.getIndex() == 0)
  321.       this.overList.update(itemName);
  322.     else
  323.       this.downList.update(itemName);
  324.   }
  325. }
  326.  
  327. function AdvControl_lastUnload() {
  328.   NAVBAR_ELEM_DATA.overImages = this.overList.getInfo();
  329.   NAVBAR_ELEM_DATA.downImages = this.downList.getInfo();
  330. }
  331.  
  332.  
  333. /* ------------------------------------------------------------------*/
  334.  
  335. function ElemListView(theItemName, theInfo, imagesPerName) {
  336.   this.elemList = new ListControl(theItemName);
  337.   this.imagesPerName = imagesPerName;
  338.   this.imgNiceNames = new Array();
  339.   this.imgInfo = new Array();
  340.   this.setElemList();
  341.   this.setInfo(theInfo);
  342. }
  343.  
  344. ElemListView.prototype.setInfo = ElemListView_setInfo;
  345. ElemListView.prototype.getInfo = ElemListView_getInfo;
  346. ElemListView.prototype.load    = ElemListView_load;
  347. ElemListView.prototype.display = ElemListView_display;
  348. ElemListView.prototype.update  = ElemListView_update;
  349. ElemListView.prototype.setElemList = ElemListView_setElemList;
  350.  
  351.  
  352. //Set the imgInfo array
  353. function ElemListView_setInfo(theInfo) {
  354.   var currInfo, currName = '';
  355.   with (this) {
  356.     for (var i=0; i < theInfo.length; i++) {
  357.       if (i % (imagesPerName+1) == 0) {
  358.         currName = theInfo[i];
  359.         currInfo = '';
  360.         for (var j=0; j < imgInfo.length; j++)
  361.           if (currName == imgInfo[j].name) {
  362.             currInfo = imgInfo[j];
  363.             break;
  364.           }
  365.       } else {
  366.         if (currInfo) {
  367.           if (!currInfo.images) currInfo.images = new Array();
  368.           currInfo.images.push(theInfo[i]);
  369.   } } } }
  370. }
  371.  
  372.  
  373. //Get the array of image names and values
  374. function ElemListView_getInfo() {
  375.   var newName, retVal = new Array();
  376.   with (this) {
  377.     for (var i=0; i < imgInfo.length; i++) {
  378.       if (imgInfo[i].images) {
  379.         imgInfo[i].name = imgInfo[i].obj.name;
  380.         if (!imgInfo[i].name) { // name the image
  381.           newName = getUniqueName("Image", createObjsArray("IMG"));
  382.           imgInfo[i].obj.name = newName;
  383.           imgInfo[i].name = newName;
  384.         }
  385.         retVal.push(imgInfo[i].name);
  386.         for (var j=0; j < imagesPerName; j++)
  387.           retVal.push(imgInfo[i].images[j]);
  388.   } } }
  389.   return retVal;
  390. }
  391.  
  392.  
  393. //Display the list of images
  394. function ElemListView_load() {
  395.   with (this) {
  396.     file0 = findObject('file0');
  397.     file1 = findObject('file1');
  398.     elemList.setAll(imgNiceNames);
  399.   }
  400. }
  401.  
  402.  
  403. //Update the image list display
  404. function ElemListView_display(index) {
  405.   if (this.imgInfo.length > 0) with (this) {
  406.     index = (index != null) ? index : elemList.getIndex();
  407.  
  408.     // mark the set images
  409.     for (var i=0; i < imgInfo.length; i++) {
  410.       if (imgInfo[i].images)
  411.         elemList.set(imgNiceNames[i] + " *", i);
  412.       else
  413.         elemList.set(imgNiceNames[i], i);
  414.     }
  415.     elemList.setIndex(index);
  416.  
  417.     // display the text fields
  418.     if (imgInfo[index].images) {
  419.       file0.value = imgInfo[index].images[0];
  420.       if (imagesPerName > 1)
  421.         file1.value = imgInfo[index].images[1];
  422.     } else {
  423.       file0.value = '';
  424.       if (imagesPerName > 1) file1.value = '';
  425.   } }
  426. }
  427.  
  428.  
  429. //Update the values in the ingInfo array
  430. function ElemListView_update(itemName) {
  431.   var index = this.elemList.getIndex();
  432.   if (this.imgInfo.length > 0) with (this) {
  433.     if (itemName == 'elemList') {
  434.       display(index);
  435.     } else if (itemName == 'file0') {
  436.       if (!imgInfo[index].images) imgInfo[index].images = new Array();
  437.       imgInfo[index].images[0] = stripSpaces(file0.value);
  438.       if (imgInfo[index].images[1] == null)
  439.         imgInfo[index].images[1] = '';
  440.       display(index);
  441.     } else if (itemName == 'file1') {
  442.       if (!imgInfo[index].images) imgInfo[index].images = new Array();
  443.       imgInfo[index].images[1] = stripSpaces(file1.value);
  444.       display(index);
  445.     }
  446.     if (imgInfo[index].images &&
  447.         !imgInfo[index].images[0] && !imgInfo[index].images[1])
  448.       imgInfo[index].images = '';
  449.   }
  450. }
  451.  
  452.  
  453. //Set the imgNiceNames and imgNames arrays
  454. function ElemListView_setElemList() {
  455.   var name, refArray, objArray, node, niceNameSrcArray = new Array();
  456.   var nsRefs = getAllObjectRefs("NS 4.0","IMG");
  457.   var ieRefs = getAllObjectRefs("IE 4.0","IMG");
  458.  
  459.   //Search for unreferenceable objects.
  460.   // <DIV id="foo"> is IE only, <LAYER> is NS only.
  461.   //if REF_CANNOT found, return empty string, and use IE refs for nice namelist.
  462.   refArray = nsRefs;
  463.   for (var i=0; i < nsRefs.length; i++) {
  464.     if (ieRefs[i].indexOf(REF_CANNOT) == 0)
  465.       ieRefs[i] = ""; //blank it out
  466.     if (nsRefs[i].indexOf(REF_CANNOT) == 0) {
  467.       nsRefs[i] = ""; //blank it out
  468.       refArray = ieRefs; //use the IE list
  469.     }
  470.   }
  471.   objArray = createObjsArray("IMG");
  472.   for (var i=0; i < refArray.length; i++) {
  473.     if (refArray[i]) {
  474.       name = getNameFromRef(refArray[i]);
  475.       if (name != NAVBAR_ELEM_DATA.imgName && objArray[i] != NAVBAR_ELEM_DATA.tagObj) {
  476.         node = new Object();
  477.         node.name = name;
  478.         node.images = '';
  479.         node.obj = objArray[i];
  480.         this.imgInfo.push(node);
  481.         niceNameSrcArray.push(refArray[i]);
  482.   } } }
  483.   this.imgNiceNames = niceNames(niceNameSrcArray, TYPE_Image);
  484. }
  485.  
  486. //Return an array of objects with the given tag name in this document and
  487. // its enclosing frames.
  488. function createObjsArray(tagName) {
  489.   var frameListLen,objsArray=new Array(),thisFrame;
  490.   if (dw.getDocumentDOM('parent')){//if frames
  491.     frameListLen = dw.getDocumentDOM('parent').getElementsByTagName('frame').length;
  492.     for (i=0;i<frameListLen;i++){
  493.       thisFrame = 'parent.frames[' + i + ']';
  494.       objsArray = objsArray.concat(dw.getDocumentDOM(thisFrame).getElementsByTagName(tagName));
  495.     }
  496.   } else //if no frames
  497.     objsArray = dw.getDocumentDOM("document").getElementsByTagName(tagName);
  498.   return objsArray;
  499. }
  500.  
  501. //Return a unique name based on the tagString
  502. function getUniqueName(tagString,tagNameObjsArray) {
  503.   var frameListSize,objName,dupe=true,counter=1;
  504.   while (dupe==true){ //check new name against name of all other tagName objs
  505.     dupe=false;
  506.     objName = tagString + counter++;
  507.     //iterates through possible names: tagName1, then tagName2, etc.
  508.     for (var i=0; dupe == false && i < tagNameObjsArray.length; i++){
  509.       //if another object of this type has the same name
  510.       if (tagNameObjsArray[i].getAttribute("name") == objName)
  511.         dupe=true; //then repeat the loop, trying a new name
  512.   } }
  513.   return objName; //return new name
  514. }
  515.